package com.ag.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

/**
 * @author <a href="mailto:rplees.i.ly@gmail.com">rplees</a> date 2010-04-06
 *         {@code} 一些常用的集合有关的帮助方法
 */
public class CollectionUtils {

	/**
	 * 判断集合是否为空
	 * 
	 * @param collection
	 * @return true为空，false不为空
	 */
	public static boolean isEmpty(Collection<?> collection) {
		return collection == null || collection.isEmpty();
	}

	public static boolean isEmpty(Iterable<?> collection) {
		return collection == null || !collection.iterator().hasNext();
	}

	/**
	 * 判断Map集合是否为空
	 * 
	 * @param m
	 * @return true为空，false不为空
	 */
	public static boolean isEmpty(Map<?, ?> m) {
		return m == null || m.size() < 1;
	}

	public static boolean isEmpty(Object[] o) {
		return o == null || o.length < 1;
	}

	public static boolean isNotEmpty(Object[] o) {
		return !isEmpty(o);
	}

	/**
	 * 判断集合是否不为空
	 * 
	 * @param collection
	 * @return true为非空，false为空
	 */
	public static boolean isNotEmpty(Map<?, ?> m) {
		return m != null && m.size() > 0;
	}

	/**
	 * 判断Map集合是否不为空
	 * 
	 * @param m
	 * @return true为非空，false为空
	 */
	public static boolean isNotEmpty(Iterable<?> coll) {
		return coll != null && coll.iterator().hasNext();
	}

	public static boolean isNotEmpty(Collection<?> coll) {
		return coll != null && !coll.isEmpty();
	}

	public static <K, T> T mapPutIfAbsentOrGet(Map<K, T> map, K key, T t) {
		if(!map.containsKey(key)) {
			map.put(key, t);
		}
		
		return map.get(key);
	}
	
	public static <K> K listAddIfAbsentOrGet(List<K> list, K k, Predicate<K> predicate) {
		Optional<K> optional = list.stream().filter(predicate).findAny();
		K retK = null;
		if(! optional.isPresent()) {
			list.add(k);
			retK = k;
		} else {
			retK = optional.get();
		}
		return retK;
	}
	
	public static Collection<?> convert(Object o) {
		if (o instanceof Collection) {
			return (Collection<?>) o;
		}
		if (o instanceof Object[]) {
			return Arrays.asList((Object[]) o);
		}

		throw new RuntimeException("cannot convert to Collections.Object:" + o);
	}

	/**
	 * 
	 * 俩个list比较找出相对与后者来说不同的部分
	 * 
	 * @param aList
	 * @param bList
	 * @return 不同的集合
	 */
	public static <T> List<T> compareLatterFindDiff(List<T> aList, List<T> bList) {
		List<T> ret = new ArrayList<T>();
		if (aList == bList) {
			return ret;
		}

		if (bList == null) {
			return ret;
		}
		if (aList == null) {
			return bList;
		}

		for (T bObj : bList) {
			if (!aList.contains(bObj)) {
				ret.add(bObj);
			}
		}

		return ret;
	}
}